//package org.ddd.pattern.event.consumer.interceptor.tt;
//
//import org.apache.commons.lang3.exception.ExceptionUtils;
//import org.ddd.pattern.event.domain.enums.ExecutionStatus;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.text.MessageFormat;
//import java.util.ArrayList;
//import java.util.List;
//
//public class MainSubscribeHandler implements ISubscribeHandler {
//
//    private static final Logger log = LoggerFactory.getLogger(MainSubscribeHandler.class);
//
//    public void setAdditionalHandlers(
//            List<? extends ISubscribeHandler> additionalHandlers) {
//        this.additionalHandlers = additionalHandlers;
//    }
//
//    private List<? extends ISubscribeHandler> additionalHandlers = new ArrayList<>();
//
//
//    @Override
//    public void handle(ISubscribeRequest request, ISubscribeResponse response,
//                       ISubscribeHandlerChain chain) {
//        try {
//            new VirtualSubscribeHandlerChain(additionalHandlers, chain).handle(request, response);
//        } catch (Exception ex) {
//
//            Throwable rootEx = ExceptionUtils.getRootCause(ex);
//            String pattern = "【领域事件】事件消费失败!, message_id->{0}, message_key->{1}, topic->{2}, group->{3}";
//            Object[] args = {request.getMessageContext().getMessageInfo().getMessageId()
//                    , request.getMessageContext().getMessageInfo().getMessageKey()
//                    , request.getMessageContext().getMessageInfo().getTopic()
//                    , request.getMessageContext().getMessageInfo().getConsumerGroup()};
//            log.error(MessageFormat.format(pattern, args), rootEx);
//            if (response.getSubscribeResult() == null
//                    || response.getSubscribeResult().getExecutionStatus() == ExecutionStatus.UNKNOWN) {
//                response.setSubscribeResult(SubscribeResult.FAILURE(rootEx));
//            }
//        }
//    }
//
//    @Override
//    public int order() {
//        return -1;
//    }
//
//    private static class VirtualSubscribeHandlerChain implements ISubscribeHandlerChain {
//
//        private final List<? extends ISubscribeHandler> additionalHandlers;
//        private final ISubscribeHandlerChain originalChain;
//        private int currentPosition = 0;
//
//        private VirtualSubscribeHandlerChain(
//                List<? extends ISubscribeHandler> additionalHandlers,
//                ISubscribeHandlerChain originalChain) {
//            this.additionalHandlers = additionalHandlers;
//            this.originalChain = originalChain;
//        }
//
//        @Override
//        public void handle(ISubscribeRequest request, ISubscribeResponse response) {
//
//            if (this.currentPosition == this.additionalHandlers.size()) {
//                this.originalChain.handle(request, response);
//            } else {
//                this.currentPosition++;
//                ISubscribeHandler nextHandler = this.additionalHandlers.get(this.currentPosition - 1);
//                nextHandler.handle(request, response, this);
//            }
//
//        }
//
//    }
//}
